/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Aplicacion.java
 *
 * Created on 14-05-2011, 07:54:46 PM
 */

package aplicacion;

import clases.Abrir;
import clases.Archivo;
import clases.Fecha;
import clases.Guardar;
import clases.Hora;
import clases.Redimensionar;
import clases.Rutas;
import clases.UnidadDeMedida;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import acercade.*;

/**
 *
 * @author Administrador
 */
public class Aplicacion extends javax.swing.JFrame {
    /** Creates new form Aplicacion */
    public Aplicacion() {
        initComponents();
//        ruta.setEditable(false);
        Redimensionar.redimensionarFormulario(this, true, "JIndex");
//        this.setExtendedState(Aplicacion.MAXIMIZED_HORIZ+Aplicacion.MAXIMIZED_VERT);//maximizado
        win.setBounds(0,0,(int)win.getPreferredSize().getWidth()+20,(int)win.getPreferredSize().getHeight()+50);
        win.setLocationRelativeTo(null);
        initAcercaDe();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        win = new javax.swing.JFrame();
        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ruta = new javax.swing.JTextField();
        btnExaminar = new javax.swing.JButton();
        btnEscanear = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultado = new javax.swing.JEditorPane();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        carp = new javax.swing.JLabel();
        arc = new javax.swing.JLabel();
        tamaño = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        incluir = new javax.swing.JCheckBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuItemAcercaDe = new javax.swing.JMenuItem();

        jLabel5.setText("jLabel5");

        javax.swing.GroupLayout winLayout = new javax.swing.GroupLayout(win.getContentPane());
        win.getContentPane().setLayout(winLayout);
        winLayout.setHorizontalGroup(
            winLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(winLayout.createSequentialGroup()
                .addGap(117, 117, 117)
                .addComponent(jLabel5)
                .addContainerGap(132, Short.MAX_VALUE))
        );
        winLayout.setVerticalGroup(
            winLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(winLayout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addComponent(jLabel5)
                .addContainerGap(74, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ruta"));
        jPanel1.setPreferredSize(new java.awt.Dimension(440, 230));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Ruta a Escanear:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, -1, 20));
        jPanel1.add(ruta, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, 330, -1));

        btnExaminar.setText("Examinar...");
        btnExaminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExaminarActionPerformed(evt);
            }
        });
        jPanel1.add(btnExaminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 30, -1, -1));

        btnEscanear.setText("Escanear AHORA!");
        btnEscanear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEscanearActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado Escaneo"));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        resultado.setBackground(new java.awt.Color(0, 0, 0));
        resultado.setFont(new java.awt.Font("Monospaced", 1, 13));
        resultado.setForeground(new java.awt.Color(0, 255, 51));
        jScrollPane1.setViewportView(resultado);

        jPanel2.add(jScrollPane1);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Información"));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("carpetas, ");
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 20, -1, -1));

        jLabel3.setText("archivos");
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 20, -1, -1));

        jLabel4.setText("Tamaño Total: ");
        jPanel3.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        carp.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        carp.setText(" ");
        jPanel3.add(carp, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 20, 40, -1));

        arc.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        arc.setText(" ");
        jPanel3.add(arc, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 20, 50, -1));

        tamaño.setFont(new java.awt.Font("Tahoma", 1, 12));
        tamaño.setText(" ");
        jPanel3.add(tamaño, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 200, -1));

        jButton2.setText("Guardar Registro");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        incluir.setText("Incluir Archivos");

        jMenu1.setText("Ayuda");

        menuItemAcercaDe.setText("Acerca de...");
        jMenu1.add(menuItemAcercaDe);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnEscanear)
                .addGap(32, 32, 32)
                .addComponent(incluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 260, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEscanear, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(incluir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExaminarActionPerformed
        if(Abrir.abrirUbicacion("Aceptar")){
            ruta.setText(Abrir.getRuta());
        }
    }//GEN-LAST:event_btnExaminarActionPerformed

    private void btnEscanearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEscanearActionPerformed
        new Thread(new Runnable() {

            public void run() {
                btnEscanear.setEnabled(false);
                btnEscanear.setText("Escaneando...");
                Rutas.setResultado("");
                Rutas.setContadoresEnCero();

                if(incluir.isSelected()){
                    resultado.setText(Rutas.imprimirTodo(new File(ruta.getText())));
                }else{
                    resultado.setText(Rutas.imprimirSoloCarpetas(new File(ruta.getText())));
                }



                double tama = Rutas.getTamaño(UnidadDeMedida.MEGABYTE);

                if(tama > 1000){
                    tamaño.setText(cambiarDecimalFloat(Rutas.getTamaño(UnidadDeMedida.GIGABYTE),1)+" GB ("+Rutas.getTam()+" bytes)");
                }else{
                    tamaño.setText(cambiarDecimalFloat(Rutas.getTamaño(UnidadDeMedida.MEGABYTE),1)+" MB ("+Rutas.getTam()+" bytes)");
                }

                arc.setText(Integer.toString(Rutas.getCantidadArchivos()));
                carp.setText(Integer.toString(Rutas.getCantidadCarpetas()));
                btnEscanear.setEnabled(true);
                btnEscanear.setText("Escanear AHORA!");
            }
        }).start();
    }//GEN-LAST:event_btnEscanearActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(Guardar.guardarComo("Escaneo_"+Fecha.getFecha(true, false), "rtf", "Guardar Escaneo", "/")){
            try {
                Archivo.escribirEnAchivo(Guardar.getRuta(), Rutas.getResultado()+"\n\n\nTamaño Total: "+tamaño.getText()+"\t"+carp.getText()+" carpetas, "+arc.getText()+" archivos", false, false);
            } catch (IOException ex) {
                Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Aplicacion().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel arc;
    private javax.swing.JButton btnEscanear;
    private javax.swing.JButton btnExaminar;
    private javax.swing.JLabel carp;
    private javax.swing.JCheckBox incluir;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem menuItemAcercaDe;
    private javax.swing.JEditorPane resultado;
    private javax.swing.JTextField ruta;
    private javax.swing.JLabel tamaño;
    private javax.swing.JFrame win;
    // End of variables declaration//GEN-END:variables

    private String cambiarDecimalFloat(double numero, int decimales) {
        String texto = Double.toString(numero);
        try{
            
            String[] n;
            if(texto.contains(".")){
                n = texto.split("\\.");
            }else{
                n = texto.split("\\,");
            }

            texto = n[0] +","+ n[1].substring(0, decimales);
            return texto;
        }catch(Exception e){
            decimales--;
            cambiarDecimalFloat(numero,decimales);
        }finally{
            return texto;
        }
        
    }

    private void initAcercaDe() {
        AcercaDe ad = new AcercaDe("JIndex", "Programa hecho en Java para examinar una ruta específica y ver sus contenidos", "Patricio Pérez Pinto", "2012", "Patricio Pérez Pinto", GPL.AMBAS_VERSIONES);
        ad.addComponent(this.menuItemAcercaDe);
    }
    
}
